package com.ranshiyan.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ranshiyan.dao.UserMapper;
import com.ranshiyan.entity.User;
import com.ranshiyan.service.UserService;
import com.ranshiyan.utils.CmsContant;
import com.ranshiyan.utils.CmsUtils;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	UserMapper userMapper;


	@Override
	public User getUserById(Integer userId) {
		// TODO Auto-generated method stub
		return userMapper.getById(userId);
	}

	@Override
	public int updateStatus(Integer userId, int status) {
		// TODO Auto-generated method stub
		return userMapper.updateStatus(userId, status);
	}

	@Override
	public User findByName(String username) {
		// TODO Auto-generated method stub
		return userMapper.findByName(username);
	}

	@Override
	public int register(User user) {
		// TODO Auto-generated method stub
		user.setPassword(CmsUtils.md5Encrytype(user.getPassword(),user.getUsername()));
		return userMapper.add(user);
	}

	@Override
	public User login(User user) {
		// TODO Auto-generated method stub
		User loginUser = findByName(user.getUsername());
		String pwdmd5 = CmsUtils.md5Encrytype(user.getPassword(), user.getUsername());
		if(loginUser!=null&&pwdmd5.equals(loginUser.getPassword())) {
			return loginUser;
		}
		return null;
	}

	@Override
	public PageInfo<User> list(int page) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, CmsContant.PAGE_SIZE);
		return new PageInfo<User>(userMapper.list());
	}
}
